package org.despair2D.ui 
{
	import flash.display.DisplayObject;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.core.Component;
	import org.despair2D.ui.DespairUI;
	import org.despair2D.debug.Logger;
	import org.despair2D.ui.core.ComponentProxy;
	import org.despair2D.ui.core.FusionComp
	import org.despair2D.ui.puppet.*;
	
	import org.despair2D.core.ns_despair
	use namespace ns_despair;
	
	/**
	 * @usage
	 * 傀儡种类 :
	 * 
	 * 1. AnimePuppet(動画)
	 * 2. ImagePuppet(純粋の画像)
	 * 3. NineScalePuppet(拡大縮小)
	 * 4. DisplayObjectContainerPuppet(显示容器傀儡)
	 * 5. InputPuppet(输入)
	 */
public class Fusion extends ComponentProxy implements IFusion
{
	
	public function Fusion()
	{
		m_fusionComp = FusionComp.getFusionComp(this);
	}
	
	
	
	final override public function set rotation ( v:Number ) : void 
	{
		super.rotation = v;
		
		if (!m_transformDirty)
		{
			this.makeTransform();
		}
	}
	
	
	final override public function get hovered() : Boolean
	{
		return Boolean(DespairUI.overFusion == this);
	}
	
	
	final override public function get pressed() : Boolean
	{
		return Boolean(DespairUI.pressedFusion == this);;
	}
	
	
	
	final public function get group() : FusionGroup
	{
		return m_group;
	}
	
	
	final public function get numPuppets() : int
	{
		return m_fusionComp.m_numPuppets
	}
	
	
	final public function createAnimePuppet( sectionName:String, index:int = -1, name:String = null ) : AnimePuppet
	{
		return m_fusionComp.createAnimePuppet(sectionName, index, name);
	}
	
	
	
	final public function createImagePuppet( isCenter:Boolean = false, index:int = -1, name:String = null ) : ImagePuppet
	{
		return m_fusionComp.createImagePuppet( isCenter, index, name);
	}
	
	
	final public function createNineScalePuppet( displayRef:*, width:Number = -1, height:Number = -1, index:int = -1, name:String = null ) : NineScalePuppet
	{
		return m_fusionComp.createNineScalePuppet(displayRef, width, height, index, name);
	}
	
	
	final public function createDisplayObjectContainerPuppet( cacheAsBitmap:Boolean = true, index:int = -1, name:String = null ) : DisplayObjectContainerPuppet
	{
		return m_fusionComp.createDisplayObjectContainerPuppet( cacheAsBitmap, index, name);
	}
	

	final public function createTextPuppet( text:String, offsetX:Number = 0, offsetY:Number = 0, embedFont:Boolean = false, index:int = -1, name:String = null ) : TextPuppet
	{
		return m_fusionComp.createTextPuppet(text, offsetX, offsetY, embedFont, index, name);
	}
	
	
	final public function getPuppetByName( name:String ) : IPuppet 
	{
		return m_fusionComp.getPuppetByName(name);
	}
	
	
	final public function getPuppetAt( index:int = 0 ) : IPuppet
	{
		return m_fusionComp.getPuppetAt(index)
	}
	
	
	final public function removeAllPuppets() : void
	{
		m_fusionComp.removeAllPuppets();
	}
	
	
	final override public function kill() : void
	{
		if (m_group)
		{
			m_group.removeProxy(this);
		}
		
		super.kill();
	}
	
	
	
	
	final override ns_despair function get view() : IView
	{
		return m_fusionComp;
	}
	
	
	ns_despair function makeTransform() : void
	{
		m_transformDirty = true;
		
		var l:int = m_fusionComp.m_numPuppets;
		while (--l > -1)
		{
			m_fusionComp.m_puppetList[l].m_view.makeTransform();
		}
	}
	
	
	final ns_despair function setGroup( v:FusionGroup, name:String ) : void
	{
		if (m_group)
		{
			Logger.reportError(this, 'setGroup', '已加入合体组: ' + m_group.name);
		}
		m_group         =  v;
		this.view.name  =  name
	}
	

	override ns_despair function recycle() : void
	{
		super.recycle();
		m_group       =  null
		m_fusionComp  =  null
	}
	
	
	
	
	ns_despair var m_fusionComp:FusionComp
	
	ns_despair var m_group:FusionGroup
	
	ns_despair var m_transformDirty:Boolean;

}

}